package com.iyoudoock.citymine.activity.setting;

import android.os.Bundle;
import android.view.View;

import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.activity.setting.AboutActivity;
import com.iyoudoock.citymine.activity.setting.FeedbackActivity;
import com.iyoudoock.citymine.activity.setting.HelpActivity;
import com.iyoudoock.citymine.constants.HttpUtils;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.kvin.toolkit.utils.JumpUtils;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.http.JsonHttpResponseHandler;
import com.kvin.toolkit.utils.http.JsonUtils;
import com.kvin.toolkit.widget.ItemLinearLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AppSettingActivity extends BaseActivity {

    @BindView(R.id.about_us_container)
    ItemLinearLayout aboutUsContainer;
    @BindView(R.id.help_container)
    ItemLinearLayout helpContainer;
    @BindView(R.id.feedback_container)
    ItemLinearLayout feedbackContainer;
    @BindView(R.id.update_container)
    ItemLinearLayout updateContainer;
    @BindView(R.id.clear_cathe_container)
    ItemLinearLayout clearCatheContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_setting);
        ButterKnife.bind(this);
    }

    @Override
    public void initNav() {
        setTitle("应用设置");
    }

    @Override
    public void init() {
        mJsonUtils = JsonUtils.getInstance();
        this.aboutUsContainer.setOnClickListener(this);
        this.helpContainer.setOnClickListener(this);
        this.feedbackContainer.setOnClickListener(this);
        this.updateContainer.setOnClickListener(this);
        this.clearCatheContainer.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.about_us_container:
                JumpUtils.simpleJump(getActivity(), AboutActivity.class);
                break;
            case R.id.help_container:
                loadingHelpInfo();
                break;
            case R.id.feedback_container:
                JumpUtils.simpleJump(getActivity(), FeedbackActivity.class);
                break;
            case R.id.update_container:
                ToastUtils.simpleToast(getActivity(), "Umeng自动更新服务已关闭");
                break;
            case R.id.clear_cathe_container:
                ToastUtils.simpleToast(getActivity(), "暂无处理");
                break;
        }
    }

    /**
     * loading help info
     */
    private void loadingHelpInfo() {
        HashMap<String, String> params = HttpUtils.helpAndLaw("help");
        mJsonUtils.post(HttpUtils.HELP_URL, params, this, Value.Flag.HELP_OR_LAW);
    }


    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            if (flag.equals(Value.Flag.HELP_OR_LAW)) {
                String url = response.getJSONObject(Key.Tag.DATA).getString(Key.Tag.URL);
                mBundle = new Bundle();
                mBundle.putString(Key.Tag.URL, url);
                mBundle.putString(Key.Tag.TITLE, "使用帮助");
                JumpUtils.jumpWithParams(getActivity(), HelpActivity.class, mBundle);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(IOException e, String flag) {
        super.onFailure(e, flag);
    }
}
